package com.demo.livemanagement.timertask;

import java.util.Date;

import org.apache.log4j.Logger;

import com.demo.livemanagement.domain.LiveInformation;
import com.demo.livemanagement.service.LiveService;
import com.demo.utils.applicationcontext.SpringContextUtil;

/**
 * 直播结束自动监控线程
 * 
 * @author Rose 2017年11月7日
 */
public class LiveTimeoutThreadTimerTask implements Runnable {

	private static final Logger LOGGER = Logger.getLogger(LiveTimeoutThreadTimerTask.class);
	private LiveService liveService = (LiveService) SpringContextUtil.getBean(LiveService.class);
	private LiveQueue<LiveInformation> qu = LiveQueue.getInstance();

	@Override
	public void run() {

		LOGGER.info(" ====live thread start success====");

		while (true) {
			// synchronized (this) {
			try {
				LiveInformation obj = (LiveInformation) qu.peek();
				if (obj == null || obj.getLiveEndTime().after(new Date())) {
//					Thread.currentThread();
//					System.err.println("LiveDelayQueue size:" + qu.size() + ",访问线程为：" + Thread.currentThread().getName()
//							+ ",时间为：" + new Date());
					Thread.sleep(5000);
					continue;
				}
				qu.poll();
				doUpdateService(obj);
				LOGGER.info("直播过期自动处理成功，直播主键是：" + obj.getLiveMainId());
			} catch (Exception e) {
				LOGGER.error("直播过期处理线程出错：", e);
			}
		}

	}
	// }

	private void doUpdateService(LiveInformation obj) {
		liveService.timerUpdateLive(obj.getLiveMainId());
	}

}
